package com.gitee.mocoffee.embedredis.spring;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;
import redis.embedded.RedisServer;

/**
 * @author lijia
 */
@Configuration
@ConditionalOnProperty(name = "app.embedredis.enable", havingValue = "true")
@FieldDefaults(level = AccessLevel.PRIVATE)
public class EmbedRedisConfig {
    @Value("${app.embedredis.port}")
    Integer port;

    @Bean(initMethod = "start", destroyMethod = "stop")
    @Order(Integer.MIN_VALUE)
    public RedisServer getRedisServer() throws Exception {
        return port != null && port > 0 ? new RedisServer(port) : new RedisServer();
    }
}
